package network;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

import server.Entity;
import server.Entity.EntityType;
import shared.Vec2f;
import vmo.PlayerData;

import network.Packet.packettype;

public class Packet_Player extends Packet {
	public short m_ewidth, m_eheight;
	public EntityType m_Etype;
	public PlayerData m_player;
	
	
	public Packet_Player(int timestamp,InetAddress address, PlayerData data)
	{
		m_address=address;
		m_timestamp=timestamp;
		m_type=packettype.PLAYER;
		m_player=data;
	}
	public Packet_Player(DatagramPacket pack)
	{
		ByteBuffer buffer=ByteBuffer.wrap(pack.getData());
		Header(pack.getAddress(),buffer);	
		m_player=new PlayerData();
		m_player.m_entity=buffer.getShort();
		m_ewidth=buffer.getShort();
		m_eheight=buffer.getShort();
		m_player.m_layer=buffer.getShort();
		m_player.m_pos=new Vec2f(buffer.getFloat(),buffer.getFloat());
		m_Etype=Entity.getTypefromByte(buffer.get());
	}
	
	public DatagramPacket Encode(InetAddress address, int port)
	{
		byte []buffer=new byte[1024];
		ByteBuffer BB=ByteBuffer.wrap(buffer);
		//encode type
		BB.put(getTypebyte(m_type));
		BB.putInt(m_timestamp);
		BB.putShort(m_player.m_entity);
		BB.putShort(m_ewidth);
		BB.putShort(m_eheight);
		BB.putShort(m_player.m_layer);
		BB.putFloat(m_player.m_pos.x);
		BB.putFloat(m_player.m_pos.y);
		BB.put(Entity.getTypeByte(m_Etype));
		
		return new DatagramPacket(buffer,buffer.length,address,port);
	}	
	
}
